/**
* @file acl_rt.cpp
*
* Copyright (c) Huawei Technologies Co., Ltd. 2025. All Rights reserved.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

#include "runtime/set_device_vxx.h"
#include "runtime/acl_rt_impl.h"
#include "framework/executor/ge_executor.h"
#include "register/stream_manage_func_registry.h"
#include "platform/platform_info.h"
#include "common/common_inner.h"
#include "common/log_inner.h"
#include "common/error_codes_inner.h"
#include "toolchain/profiling_manager.h"

namespace {
    constexpr int32_t MODULE_TYPE_VECTOR_CORE = 7;
    constexpr int32_t MODULE_TYPE_AICORE = 4;
    constexpr int32_t INFO_TYPE_CORE_NUM = 3;
    std::mutex g_platformInfoInitMutex;
    std::unordered_set<int32_t> g_platformInfoInitSet;
}

namespace acl {
    aclError UpdatePlatformInfoWithDevice(int32_t deviceId)
    {
#ifdef __GNUC__
        // init platform info
        const aclError ret = acl::InitSocVersion();
        if (ret != ACL_SUCCESS) {
            return ret;
        }
        const string &socVersion = acl::GetSocVersion();
        if (fe::PlatformInfoManager::GeInstance().InitRuntimePlatformInfos(socVersion) != 0U) {
            ACL_LOG_WARN("[Init][PlatformInfo]init runtime platform info unsuccessfully, SocVersion = %s",
                                socVersion.c_str());
            return ACL_ERROR_INTERNAL_ERROR;
        }

        const std::unique_lock<std::mutex> lk(g_platformInfoInitMutex);
        if (g_platformInfoInitSet.count(deviceId) > 0U) {
            return ACL_SUCCESS;
        }

        uint32_t aicCnt = 0U;
        auto rtErr = rtGetAiCoreCount(&aicCnt);
        if (rtErr != RT_ERROR_NONE) {
            ACL_LOG_WARN("get aicore count unsuccessfully, runtime result = %d", static_cast<int32_t>(rtErr));
            return ACL_GET_ERRCODE_RTS(rtErr);
        }

        int64_t vecCoreCnt = 0U;
        // some chips has no vector core
        rtErr = rtGetDeviceInfo(static_cast<uint32_t>(deviceId), MODULE_TYPE_VECTOR_CORE,
                                INFO_TYPE_CORE_NUM, &vecCoreCnt);
        if (rtErr != RT_ERROR_NONE) {
            ACL_LOG_WARN("get vector core count unsuccessfully, runtime result = %d", static_cast<int32_t>(rtErr));
            return ACL_GET_ERRCODE_RTS(rtErr);
        }

        int64_t cubeCoreCnt = 0U;
        rtErr = rtGetDeviceInfo(static_cast<uint32_t>(deviceId), MODULE_TYPE_AICORE,
                                INFO_TYPE_CUBE_NUM, &cubeCoreCnt);
        if (rtErr != RT_ERROR_NONE) {
            ACL_LOG_WARN("get cube core count unsuccessfully, runtime result = %d", static_cast<int32_t>(rtErr));
            return ACL_GET_ERRCODE_RTS(rtErr);
        }

        fe::PlatFormInfos platformInfos;
        uint32_t platformRet =
            fe::PlatformInfoManager::GeInstance().GetRuntimePlatformInfosByDevice(deviceId, platformInfos);
        if (platformRet != 0U) {
            ACL_LOG_WARN("get runtime platformInfos by device unsuccessfully, deviceId = %d", deviceId);
            return ACL_ERROR_INTERNAL_ERROR;
        }

        const std::string socInfoKey = "SoCInfo";
        const std::string aicCntKey = "ai_core_cnt";
        const std::string vecCoreCntKey = "vector_core_cnt";
        const std::string cubeCoreCntKey = "cube_core_cnt";
        std::map<std::string, std::string> res;
        if (!platformInfos.GetPlatformResWithLock(socInfoKey, res)) {
            ACL_LOG_WARN("unable to get platform result");
            return ACL_ERROR_INTERNAL_ERROR;
        }

        res[aicCntKey] = std::to_string(aicCnt);
        res[vecCoreCntKey] = std::to_string(vecCoreCnt);
        res[cubeCoreCntKey] = std::to_string(cubeCoreCnt);
        platformInfos.SetPlatformResWithLock(socInfoKey, res);
        platformRet =
            fe::PlatformInfoManager::GeInstance().UpdateRuntimePlatformInfosByDevice(deviceId, platformInfos);
        if (platformRet != 0U) {
            ACL_LOG_WARN("update runtime platformInfos by device unsuccessfully, deviceId = %d", deviceId);
            return ACL_ERROR_INTERNAL_ERROR;
        }
        (void)g_platformInfoInitSet.emplace(deviceId);
        ACL_LOG_INFO("Successfully to UpdatePlatformInfoWithDevice, deviceId = %d, aicCnt = %u, vecCoreCnt = %ld, "
                     "cubeCoreCnt = %ld", deviceId, aicCnt, vecCoreCnt, cubeCoreCnt);
#endif
        return ACL_SUCCESS;
    }
}

aclError aclrtPeekAtLastError(aclrtLastErrLevel level)
{
    return aclrtPeekAtLastErrorImpl(level);
}

aclError aclrtGetLastError(aclrtLastErrLevel level)
{
    return aclrtGetLastErrorImpl(level);
}

aclError aclrtSetExceptionInfoCallback(aclrtExceptionInfoCallback callback)
{
    return aclrtSetExceptionInfoCallbackImpl(callback);
}

uint32_t aclrtGetTaskIdFromExceptionInfo(const aclrtExceptionInfo *info)
{
    return aclrtGetTaskIdFromExceptionInfoImpl(info);
}

uint32_t aclrtGetStreamIdFromExceptionInfo(const aclrtExceptionInfo *info)
{
    return aclrtGetStreamIdFromExceptionInfoImpl(info);
}

uint32_t aclrtGetThreadIdFromExceptionInfo(const aclrtExceptionInfo *info)
{
    return aclrtGetThreadIdFromExceptionInfoImpl(info);
}

uint32_t aclrtGetDeviceIdFromExceptionInfo(const aclrtExceptionInfo *info)
{
    return aclrtGetDeviceIdFromExceptionInfoImpl(info);
}

uint32_t aclrtGetErrorCodeFromExceptionInfo(const aclrtExceptionInfo *info)
{
    return aclrtGetErrorCodeFromExceptionInfoImpl(info);
}

aclError aclrtSubscribeReport(uint64_t threadId, aclrtStream stream)
{
    return aclrtSubscribeReportImpl(threadId, stream);
}

aclError aclrtLaunchCallback(aclrtCallback fn, void *userData, aclrtCallbackBlockType blockType, aclrtStream stream)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtLaunchCallback);
    return aclrtLaunchCallbackImpl(fn, userData, blockType, stream);
}

aclError aclrtProcessReport(int32_t timeout)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtProcessReport);
    return aclrtProcessReportImpl(timeout);
}

aclError aclrtUnSubscribeReport(uint64_t threadId, aclrtStream stream)
{
    return aclrtUnSubscribeReportImpl(threadId, stream);
}

aclError aclrtCreateContext(aclrtContext *context, int32_t deviceId)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtCreateContext);
    const aclError aclErr = aclrtCreateContextImpl(context, deviceId);
    if (aclErr != ACL_SUCCESS) {
        return aclErr;
    }
    // update platform info
    const auto err = acl::UpdatePlatformInfoWithDevice(deviceId);
    if (err != ACL_SUCCESS) {
        ACL_LOG_WARN("update platform info with device failed, error code is [%d], deviceId is [%d]", err, deviceId);
    }
    return aclErr;
}

aclError aclrtDestroyContext(aclrtContext context)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtDestroyContext);
    if (context != nullptr) {
        ge::MngResourceHandle contextHandle = {.context = static_cast<rtContext_t>(context)};
        (void)ge::StreamMngFuncRegistry::GetInstance().TryCallStreamMngFunc(
            ge::StreamMngFuncType::ACLNN_STREAM_CALLBACK, ge::MngActionType::DESTROY_CONTEXT, contextHandle);
        ACL_LOG_INFO("try to call stream manage function success, action type is DESTROY_CONTEXT.");
    }
    return aclrtDestroyContextImpl(context);
}

aclError aclrtSetCurrentContext(aclrtContext context)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtSetCurrentContext);
    return aclrtSetCurrentContextImpl(context);
}

aclError aclrtGetCurrentContext(aclrtContext *context)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtGetCurrentContext);
    return aclrtGetCurrentContextImpl(context);
}

aclError aclrtCtxGetSysParamOpt(aclSysParamOpt opt, int64_t *value)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtCtxGetSysParamOpt);
    return aclrtCtxGetSysParamOptImpl(opt, value);
}

aclError aclrtCtxSetSysParamOpt(aclSysParamOpt opt, int64_t value)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtCtxSetSysParamOpt);
    return aclrtCtxSetSysParamOptImpl(opt, value);
}

aclError aclrtGetSysParamOpt(aclSysParamOpt opt, int64_t *value)
{
    return aclrtGetSysParamOptImpl(opt, value);
}

aclError aclrtSetSysParamOpt(aclSysParamOpt opt, int64_t value)
{
    return aclrtSetSysParamOptImpl(opt, value);
}

aclError aclrtSetDevice(int32_t deviceId)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtSetDevice);
    const aclError aclErr = aclrtSetDeviceImpl(deviceId);
    if (aclErr != ACL_SUCCESS) {
        return aclErr;
    }
    // update platform info
    const auto err = acl::UpdatePlatformInfoWithDevice(deviceId);
    if (err != ACL_SUCCESS) {
        ACL_LOG_WARN("update platform info with device failed, error code is [%d], deviceId is [%d]", err, deviceId);
    }
    return aclErr;
}

aclError aclrtResetDevice(int32_t deviceId)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtResetDevice);
    return aclrtResetDeviceImpl(deviceId);
}

aclError aclrtResetDeviceForce(int32_t deviceId)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtResetDeviceForce);
    return aclrtResetDeviceForceImpl(deviceId);
}

aclError aclrtGetDevice(int32_t *deviceId)
{
    return aclrtGetDeviceImpl(deviceId);
}

aclError aclrtSetStreamFailureMode(aclrtStream stream, uint64_t mode)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtSetStreamFailureMode);
    return aclrtSetStreamFailureModeImpl(stream, mode);
}

aclError aclrtGetRunMode(aclrtRunMode *runMode)
{
    return aclrtGetRunModeImpl(runMode);
}

aclError aclrtSynchronizeDevice(void)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtSynchronizeDevice);
    return aclrtSynchronizeDeviceImpl();
}

aclError aclrtSynchronizeDeviceWithTimeout(int32_t timeout)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtSynchronizeDeviceWithTimeout);
    return aclrtSynchronizeDeviceWithTimeoutImpl(timeout);
}

aclError aclrtSetTsDevice(aclrtTsId tsId)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtSetTsDevice);
    return aclrtSetTsDeviceImpl(tsId);
}

aclError aclrtGetDeviceUtilizationRate(int32_t deviceId, aclrtUtilizationInfo *utilizationInfo)
{
    return aclrtGetDeviceUtilizationRateImpl(deviceId, utilizationInfo);
}

aclError aclrtGetDeviceCount(uint32_t *count)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtGetDeviceCount);
    return aclrtGetDeviceCountImpl(count);
}

aclError aclrtCreateEvent(aclrtEvent *event)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtCreateEvent);
    return aclrtCreateEventImpl(event);
}

aclError aclrtCreateEventWithFlag(aclrtEvent *event, uint32_t flag)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtCreateEventWithFlag);
    return aclrtCreateEventWithFlagImpl(event, flag);
}

aclError aclrtCreateEventExWithFlag(aclrtEvent *event, uint32_t flag)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtCreateEventExWithFlag);
    return aclrtCreateEventExWithFlagImpl(event, flag);
}

aclError aclrtDestroyEvent(aclrtEvent event)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtDestroyEvent);
    return aclrtDestroyEventImpl(event);
}

aclError aclrtRecordEvent(aclrtEvent event, aclrtStream stream)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtRecordEvent);
    return aclrtRecordEventImpl(event, stream);
}

aclError aclrtResetEvent(aclrtEvent event, aclrtStream stream)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtResetEvent);
    return aclrtResetEventImpl(event, stream);
}

aclError aclrtQueryEvent(aclrtEvent event, aclrtEventStatus *status)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtQueryEvent);
    return aclrtQueryEventImpl(event, status);
}

aclError aclrtQueryEventStatus(aclrtEvent event, aclrtEventRecordedStatus *status)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtQueryEventStatus);
    return aclrtQueryEventStatusImpl(event, status);
}

aclError aclrtQueryEventWaitStatus(aclrtEvent event, aclrtEventWaitStatus *status)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtQueryEventWaitStatus);
    return aclrtQueryEventWaitStatusImpl(event, status);
}

aclError aclrtSynchronizeEvent(aclrtEvent event)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtSynchronizeEvent);
    return aclrtSynchronizeEventImpl(event);
}

aclError aclrtSynchronizeEventWithTimeout(aclrtEvent event, int32_t timeout)
{
    return aclrtSynchronizeEventWithTimeoutImpl(event, timeout);
}

aclError aclrtEventElapsedTime(float *ms, aclrtEvent startEvent, aclrtEvent endEvent)
{
    return aclrtEventElapsedTimeImpl(ms, startEvent, endEvent);
}

aclError aclrtEventGetTimestamp(aclrtEvent event, uint64_t *timestamp)
{
    return aclrtEventGetTimestampImpl(event, timestamp);
}

aclError aclrtMalloc(void **devPtr, size_t size, aclrtMemMallocPolicy policy)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtMalloc);
    return aclrtMallocImpl(devPtr, size, policy);
}

aclError aclrtMallocAlign32(void **devPtr, size_t size, aclrtMemMallocPolicy policy)
{
    return aclrtMallocAlign32Impl(devPtr, size, policy);
}

aclError aclrtMallocCached(void **devPtr, size_t size, aclrtMemMallocPolicy policy)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtMallocCached);
    return aclrtMallocCachedImpl(devPtr, size, policy);
}

aclError aclrtMallocWithCfg(void **devPtr, size_t size, aclrtMemMallocPolicy policy, aclrtMallocConfig *cfg)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtMallocWithCfg);
    return aclrtMallocWithCfgImpl(devPtr, size, policy, cfg);
}

aclError aclrtMallocForTaskScheduler(void **devPtr, size_t size, aclrtMemMallocPolicy policy, aclrtMallocConfig *cfg)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtMallocForTaskScheduler);
    return aclrtMallocForTaskSchedulerImpl(devPtr, size, policy, cfg);
}

aclError aclrtMallocHostWithCfg(void **ptr, uint64_t size, aclrtMallocConfig *cfg)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtMallocHostWithCfg);
    return aclrtMallocHostWithCfgImpl(ptr, size, cfg);
}

aclError aclrtPointerGetAttributes(const void *ptr, aclrtPtrAttributes *attributes)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtPointerGetAttributes);
    return aclrtPointerGetAttributesImpl(ptr, attributes);
}

aclError aclrtHostRegister(void *ptr, uint64_t size, aclrtHostRegisterType type, void **devPtr)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtHostRegister);
    return aclrtHostRegisterImpl(ptr, size, type, devPtr);
}

aclError aclrtHostUnregister(void *ptr)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtHostUnregister);
    return aclrtHostUnregisterImpl(ptr);
}

aclError aclrtGetThreadLastTaskId(uint32_t *taskId)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtGetThreadLastTaskId);
    return aclrtGetThreadLastTaskIdImpl(taskId);
}

aclError aclrtStreamGetId(aclrtStream stream, int32_t *streamId)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtStreamGetId);
    return aclrtStreamGetIdImpl(stream, streamId);
}

aclError aclrtMemFlush(void *devPtr, size_t size)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtMemFlush);
    return aclrtMemFlushImpl(devPtr, size);
}

aclError aclrtMemInvalidate(void *devPtr, size_t size)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtMemInvalidate);
    return aclrtMemInvalidateImpl(devPtr, size);
}

aclError aclrtFree(void *devPtr)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtFree);
    return aclrtFreeImpl(devPtr);
}

aclError aclrtMallocHost(void **hostPtr, size_t size)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtMallocHost);
    return aclrtMallocHostImpl(hostPtr, size);
}

aclError aclrtFreeHost(void *hostPtr)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtFreeHost);
    return aclrtFreeHostImpl(hostPtr);
}

aclError aclrtMemcpy(void *dst, size_t destMax, const void *src, size_t count, aclrtMemcpyKind kind)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpy);
    return aclrtMemcpyImpl(dst, destMax, src, count, kind);
}

aclError aclrtMemset(void *devPtr, size_t maxCount, int32_t value, size_t count)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtMemset);
    return aclrtMemsetImpl(devPtr, maxCount, value, count);
}

aclError aclrtMemcpyAsync(void *dst, size_t destMax, const void *src, size_t count, aclrtMemcpyKind kind,
    aclrtStream stream)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpyAsync);
    return aclrtMemcpyAsyncImpl(dst, destMax, src, count, kind, stream);
}

aclError aclrtMemcpyAsyncWithCondition(void *dst, size_t destMax, const void *src, size_t count, aclrtMemcpyKind kind,
    aclrtStream stream)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpyAsyncWithCondition);
    return aclrtMemcpyAsyncWithConditionImpl(dst, destMax, src, count, kind, stream);
}

aclError aclrtMemcpy2d(void *dst, size_t dpitch, const void *src, size_t spitch, size_t width, size_t height,
    aclrtMemcpyKind kind)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpy2d);
    return aclrtMemcpy2dImpl(dst, dpitch, src, spitch, width, height, kind);
}

aclError aclrtMemcpy2dAsync(void *dst, size_t dpitch, const void *src, size_t spitch, size_t width, size_t height,
    aclrtMemcpyKind kind, aclrtStream stream)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpy2dAsync);
    return aclrtMemcpy2dAsyncImpl(dst, dpitch, src, spitch, width, height, kind, stream);
}

aclError aclrtMemsetAsync(void *devPtr, size_t maxCount, int32_t value, size_t count, aclrtStream stream)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtMemsetAsync);
    return aclrtMemsetAsyncImpl(devPtr, maxCount, value, count, stream);
}

aclError aclrtReserveMemAddress(void **virPtr, size_t size, size_t alignment, void *expectPtr, uint64_t flags)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtReserveMemAddress);
    return aclrtReserveMemAddressImpl(virPtr, size, alignment, expectPtr, flags);
}

aclError aclrtReleaseMemAddress(void *virPtr)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtReleaseMemAddress);
    return aclrtReleaseMemAddressImpl(virPtr);
}

aclError aclrtMallocPhysical(aclrtDrvMemHandle *handle, size_t size, const aclrtPhysicalMemProp *prop, uint64_t flags)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtMallocPhysical);
    return aclrtMallocPhysicalImpl(handle, size, prop, flags);
}

aclError aclrtFreePhysical(aclrtDrvMemHandle handle)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtFreePhysical);
    return aclrtFreePhysicalImpl(handle);
}

aclError aclrtMapMem(void *virPtr, size_t size, size_t offset, aclrtDrvMemHandle handle, uint64_t flags)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtMapMem);
    return aclrtMapMemImpl(virPtr, size, offset, handle, flags);
}

aclError aclrtUnmapMem(void *virPtr)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtUnmapMem);
    return aclrtUnmapMemImpl(virPtr);
}

aclError aclrtCreateStream(aclrtStream *stream)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtCreateStream);
    return aclrtCreateStreamImpl(stream);
}

aclError aclrtCreateStreamWithConfig(aclrtStream *stream, uint32_t priority, uint32_t flag)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtCreateStreamWithConfig);
    return aclrtCreateStreamWithConfigImpl(stream, priority, flag);
}

aclError aclrtDestroyStream(aclrtStream stream)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtDestroyStream);
    if (stream != nullptr) {
        ge::MngResourceHandle streamHandle = {.stream = static_cast<rtStream_t>(stream)};
        (void)ge::StreamMngFuncRegistry::GetInstance().TryCallStreamMngFunc(
            ge::StreamMngFuncType::ACLNN_STREAM_CALLBACK, ge::MngActionType::DESTROY_STREAM, streamHandle);
        ACL_LOG_INFO("try to call stream manage function success, action type is DESTROY_STREAM.");
    }
    return aclrtDestroyStreamImpl(stream);
}

aclError aclrtDestroyStreamForce(aclrtStream stream)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtDestroyStreamForce);
    if (stream != nullptr) {
        ge::MngResourceHandle streamHandle = {.stream = static_cast<rtStream_t>(stream)};
        (void)ge::StreamMngFuncRegistry::GetInstance().TryCallStreamMngFunc(
            ge::StreamMngFuncType::ACLNN_STREAM_CALLBACK, ge::MngActionType::DESTROY_STREAM, streamHandle);
        ACL_LOG_INFO("try to call stream manage function success, action type is DESTROY_STREAM.");
    }
    return aclrtDestroyStreamForceImpl(stream);
}

aclError aclrtSynchronizeStream(aclrtStream stream)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtSynchronizeStream);
    return aclrtSynchronizeStreamImpl(stream);
}

aclError aclrtSynchronizeStreamWithTimeout(aclrtStream stream, int32_t timeout)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtSynchronizeStreamWithTimeout);
    return aclrtSynchronizeStreamWithTimeoutImpl(stream, timeout);
}

aclError aclrtStreamQuery(aclrtStream stream, aclrtStreamStatus *status)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtStreamQuery);
    return aclrtStreamQueryImpl(stream, status);
}

aclError aclrtStreamWaitEvent(aclrtStream stream, aclrtEvent event)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtStreamWaitEvent);
    return aclrtStreamWaitEventImpl(stream, event);
}

aclError aclrtSetGroup(int32_t groupId)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtSetGroup);
    return aclrtSetGroupImpl(groupId);
}

aclError aclrtGetGroupCount(uint32_t *count)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtGetGroupCount);
    return aclrtGetGroupCountImpl(count);
}

aclrtGroupInfo *aclrtCreateGroupInfo()
{
    return aclrtCreateGroupInfoImpl();
}

aclError aclrtDestroyGroupInfo(aclrtGroupInfo *groupInfo)
{
    return aclrtDestroyGroupInfoImpl(groupInfo);
}

aclError aclrtGetAllGroupInfo(aclrtGroupInfo *groupInfo)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtGetAllGroupInfo);
    return aclrtGetAllGroupInfoImpl(groupInfo);
}

aclError aclrtGetGroupInfoDetail(const aclrtGroupInfo *groupInfo, int32_t groupIndex, aclrtGroupAttr attr,
    void *attrValue, size_t valueLen, size_t *paramRetSize)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtGetGroupInfoDetail);
    return aclrtGetGroupInfoDetailImpl(groupInfo, groupIndex, attr, attrValue, valueLen, paramRetSize);
}

aclError aclrtDeviceCanAccessPeer(int32_t *canAccessPeer, int32_t deviceId, int32_t peerDeviceId)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtDeviceCanAccessPeer);
    return aclrtDeviceCanAccessPeerImpl(canAccessPeer, deviceId, peerDeviceId);
}

aclError aclrtDeviceEnablePeerAccess(int32_t peerDeviceId, uint32_t flags)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtDeviceEnablePeerAccess);
    return aclrtDeviceEnablePeerAccessImpl(peerDeviceId, flags);
}

aclError aclrtDeviceDisablePeerAccess(int32_t peerDeviceId)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtDeviceDisablePeerAccess);
    return aclrtDeviceDisablePeerAccessImpl(peerDeviceId);
}

aclError aclrtGetMemInfo(aclrtMemAttr attr, size_t *free, size_t *total)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtGetMemInfo);
    return aclrtGetMemInfoImpl(attr, free, total);
}

aclError aclrtSetOpWaitTimeout(uint32_t timeout)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtSetOpWaitTimeout);
    return aclrtSetOpWaitTimeoutImpl(timeout);
}

aclError aclrtSetOpExecuteTimeOut(uint32_t timeout)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtSetOpExecuteTimeOut);
    return aclrtSetOpExecuteTimeOutImpl(timeout);
}

aclError aclrtSetOpExecuteTimeOutWithMs(uint32_t timeout)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtSetOpExecuteTimeOutWithMs);
    return aclrtSetOpExecuteTimeOutWithMsImpl(timeout);
}

aclError aclrtSetStreamOverflowSwitch(aclrtStream stream, uint32_t flag)
{
    return aclrtSetStreamOverflowSwitchImpl(stream, flag);
}

aclError aclrtGetStreamOverflowSwitch(aclrtStream stream, uint32_t *flag)
{
    return aclrtGetStreamOverflowSwitchImpl(stream, flag);
}

aclError aclrtSetDeviceSatMode(aclrtFloatOverflowMode mode)
{
    return aclrtSetDeviceSatModeImpl(mode);
}

aclError aclrtGetDeviceSatMode(aclrtFloatOverflowMode *mode)
{
    return aclrtGetDeviceSatModeImpl(mode);
}

aclError aclrtGetOverflowStatus(void *outputAddr, size_t outputSize, aclrtStream stream)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtGetOverflowStatus);
    return aclrtGetOverflowStatusImpl(outputAddr, outputSize, stream);
}

aclError aclrtResetOverflowStatus(aclrtStream stream)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtResetOverflowStatus);
    return aclrtResetOverflowStatusImpl(stream);
}

aclError aclrtQueryDeviceStatus(int32_t deviceId, aclrtDeviceStatus *deviceStatus)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtQueryDeviceStatus);
    return aclrtQueryDeviceStatusImpl(deviceId, deviceStatus);
}

aclrtBinary aclrtCreateBinary(const void *data, size_t dataLen)
{
    return aclrtCreateBinaryImpl(data, dataLen);
}

aclError aclrtDestroyBinary(aclrtBinary binary)
{
    return aclrtDestroyBinaryImpl(binary);
}

aclError aclrtBinaryLoad(const aclrtBinary binary, aclrtBinHandle *binHandle)
{
    return aclrtBinaryLoadImpl(binary, binHandle);
}

aclError aclrtBinaryUnLoad(aclrtBinHandle binHandle)
{
    return aclrtBinaryUnLoadImpl(binHandle);
}

aclError aclrtBinaryGetFunction(const aclrtBinHandle binHandle, const char *kernelName, aclrtFuncHandle *funcHandle)
{
    return aclrtBinaryGetFunctionImpl(binHandle, kernelName, funcHandle);
}

aclError aclrtLaunchKernel(aclrtFuncHandle funcHandle, uint32_t blockDim, const void *argsData, size_t argsSize,
    aclrtStream stream)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtLaunchKernel);
    return aclrtLaunchKernelImpl(funcHandle, blockDim, argsData, argsSize, stream);
}

aclError aclrtMemExportToShareableHandle(aclrtDrvMemHandle handle, aclrtMemHandleType handleType, uint64_t flags,
    uint64_t *shareableHandle)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtMemExportToShareableHandle);
    return aclrtMemExportToShareableHandleImpl(handle, handleType, flags, shareableHandle);
}

aclError aclrtMemImportFromShareableHandle(uint64_t shareableHandle, int32_t deviceId, aclrtDrvMemHandle *handle)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtMemImportFromShareableHandle);
    return aclrtMemImportFromShareableHandleImpl(shareableHandle, deviceId, handle);
}

aclError aclrtMemSetPidToShareableHandle(uint64_t shareableHandle, int32_t *pid, size_t pidNum)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtMemSetPidToShareableHandle);
    return aclrtMemSetPidToShareableHandleImpl(shareableHandle, pid, pidNum);
}

aclError aclrtMemGetAllocationGranularity(aclrtPhysicalMemProp *prop, aclrtMemGranularityOptions option,
    size_t *granularity)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtMemGetAllocationGranularity);
    return aclrtMemGetAllocationGranularityImpl(prop, option, granularity);
}

aclError aclrtDeviceGetBareTgid(int32_t *pid)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtDeviceGetBareTgid);
    return aclrtDeviceGetBareTgidImpl(pid);
}

aclError aclrtCmoAsync(void *src, size_t size, aclrtCmoType cmoType, aclrtStream stream)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtCmoAsync);
    return aclrtCmoAsyncImpl(src, size, cmoType, stream);
}

aclError aclrtGetMemUceInfo(int32_t deviceId, aclrtMemUceInfo *memUceInfoArray, size_t arraySize, size_t *retSize)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtGetMemUceInfo);
    return aclrtGetMemUceInfoImpl(deviceId, memUceInfoArray, arraySize, retSize);
}

aclError aclrtDeviceTaskAbort(int32_t deviceId, uint32_t timeout)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtDeviceTaskAbort);
    return aclrtDeviceTaskAbortImpl(deviceId, timeout);
}

aclError aclrtMemUceRepair(int32_t deviceId, aclrtMemUceInfo *memUceInfoArray, size_t arraySize)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtMemUceRepair);
    return aclrtMemUceRepairImpl(deviceId, memUceInfoArray, arraySize);
}

aclError aclrtStreamAbort(aclrtStream stream)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtStreamAbort);
    return aclrtStreamAbortImpl(stream);
}

aclError aclrtBinaryLoadFromFile(const char* binPath, aclrtBinaryLoadOptions *options, aclrtBinHandle *binHandle)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtBinaryLoadFromFile);
    return aclrtBinaryLoadFromFileImpl(binPath, options, binHandle);
}

aclError aclrtBinaryGetFunctionByEntry(aclrtBinHandle binHandle, uint64_t funcEntry, aclrtFuncHandle *funcHandle)
{
    return aclrtBinaryGetFunctionByEntryImpl(binHandle, funcEntry, funcHandle);
}

aclError aclrtGetFunctionAddr(aclrtFuncHandle funcHandle, void **aicAddr, void **aivAddr)
{
    return aclrtGetFunctionAddrImpl(funcHandle, aicAddr, aivAddr);
}

aclError aclrtGetMemcpyDescSize(aclrtMemcpyKind kind, size_t *descSize)
{
    return aclrtGetMemcpyDescSizeImpl(kind, descSize);
}

aclError aclrtSetMemcpyDesc(void *desc, aclrtMemcpyKind kind, void *srcAddr, void *dstAddr, size_t count, void *config)
{
    return aclrtSetMemcpyDescImpl(desc, kind, srcAddr, dstAddr, count, config);
}

aclError aclrtMemcpyAsyncWithDesc(void *desc, aclrtMemcpyKind kind, aclrtStream stream)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtMemcpyAsyncWithDesc);
    return aclrtMemcpyAsyncWithDescImpl(desc, kind, stream);
}

aclError aclrtKernelArgsGetHandleMemSize(aclrtFuncHandle funcHandle, size_t *memSize)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtKernelArgsGetHandleMemSize);
    return aclrtKernelArgsGetHandleMemSizeImpl(funcHandle, memSize);
}

aclError aclrtKernelArgsGetMemSize(aclrtFuncHandle funcHandle, size_t userArgsSize, size_t *actualArgsSize)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtKernelArgsGetMemSize);
    return aclrtKernelArgsGetMemSizeImpl(funcHandle, userArgsSize, actualArgsSize);
}

aclError aclrtKernelArgsInit(aclrtFuncHandle funcHandle, aclrtArgsHandle *argsHandle)
{
    return aclrtKernelArgsInitImpl(funcHandle, argsHandle);
}

aclError aclrtKernelArgsInitByUserMem(aclrtFuncHandle funcHandle, aclrtArgsHandle argsHandle, void *userHostMem,
    size_t actualArgsSize)
{
    return aclrtKernelArgsInitByUserMemImpl(funcHandle, argsHandle, userHostMem, actualArgsSize);
}

aclError aclrtKernelArgsAppend(aclrtArgsHandle argsHandle, void *param, size_t paramSize,
    aclrtParamHandle *paramHandle)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtKernelArgsAppend);
    return aclrtKernelArgsAppendImpl(argsHandle, param, paramSize, paramHandle);
}

aclError aclrtKernelArgsAppendPlaceHolder(aclrtArgsHandle argsHandle, aclrtParamHandle *paramHandle)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtKernelArgsAppendPlaceHolder);
    return aclrtKernelArgsAppendPlaceHolderImpl(argsHandle, paramHandle);
}

aclError aclrtKernelArgsGetPlaceHolderBuffer(aclrtArgsHandle argsHandle, aclrtParamHandle paramHandle,
    size_t dataSize, void **bufferAddr)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtKernelArgsGetPlaceHolderBuffer);
    return aclrtKernelArgsGetPlaceHolderBufferImpl(argsHandle, paramHandle, dataSize, bufferAddr);
}

aclError aclrtKernelArgsParaUpdate(aclrtArgsHandle argsHandle, aclrtParamHandle paramHandle, void *param,
    size_t paramSize)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtKernelArgsParaUpdate);
    return aclrtKernelArgsParaUpdateImpl(argsHandle, paramHandle, param, paramSize);
}

aclError aclrtLaunchKernelWithConfig(aclrtFuncHandle funcHandle, uint32_t blockDim, aclrtStream stream,
    aclrtLaunchKernelCfg *cfg, aclrtArgsHandle argsHandle, void *reserve)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtLaunchKernelWithConfig);
    return aclrtLaunchKernelWithConfigImpl(funcHandle, blockDim, stream, cfg, argsHandle, reserve);
}

aclError aclrtKernelArgsFinalize(aclrtArgsHandle argsHandle)
{
    return aclrtKernelArgsFinalizeImpl(argsHandle);
}

aclError aclrtValueWrite(void* devAddr, uint64_t value, uint32_t flag, aclrtStream stream)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtValueWrite);
    return aclrtValueWriteImpl(devAddr, value, flag, stream);
}

aclError aclrtValueWait(void* devAddr, uint64_t value, uint32_t flag, aclrtStream stream)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtValueWait);
    return aclrtValueWaitImpl(devAddr, value, flag, stream);
}

aclError aclrtGetStreamAvailableNum(uint32_t *streamCount)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtGetStreamAvailableNum);
    return aclrtGetStreamAvailableNumImpl(streamCount);
}

aclError aclrtSetStreamAttribute(aclrtStream stream, aclrtStreamAttr stmAttrType, aclrtStreamAttrValue *value)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtSetStreamAttribute);
    return aclrtSetStreamAttributeImpl(stream, stmAttrType, value);
}

aclError aclrtGetStreamAttribute(aclrtStream stream, aclrtStreamAttr stmAttrType, aclrtStreamAttrValue *value)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtGetStreamAttribute);
    return aclrtGetStreamAttributeImpl(stream, stmAttrType, value);
}

aclError aclrtCreateNotify(aclrtNotify *notify, uint64_t flag)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtCreateNotify);
    return aclrtCreateNotifyImpl(notify, flag);
}

aclError aclrtDestroyNotify(aclrtNotify notify)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtDestroyNotify);
    return aclrtDestroyNotifyImpl(notify);
}

aclError aclrtRecordNotify(aclrtNotify notify, aclrtStream stream)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtRecordNotify);
    return aclrtRecordNotifyImpl(notify, stream);
}

aclError aclrtWaitAndResetNotify(aclrtNotify notify, aclrtStream stream, uint32_t timeout)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtWaitAndResetNotify);
    return aclrtWaitAndResetNotifyImpl(notify, stream, timeout);
}

aclError aclrtGetNotifyId(aclrtNotify notify, uint32_t *notifyId)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtGetNotifyId);
    return aclrtGetNotifyIdImpl(notify, notifyId);
}

aclError aclrtGetEventId(aclrtEvent event, uint32_t *eventId)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtGetEventId);
    return aclrtGetEventIdImpl(event, eventId);
}

aclError aclrtGetEventAvailNum(uint32_t *eventCount)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtGetEventAvailNum);
    return aclrtGetEventAvailNumImpl(eventCount);
}

aclError aclrtGetDeviceInfo(uint32_t deviceId, aclrtDevAttr attr, int64_t *value)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtGetDeviceInfo);
    return aclrtGetDeviceInfoImpl(deviceId, attr, value);
}

aclError aclrtDeviceGetStreamPriorityRange(int32_t *leastPriority, int32_t *greatestPriority)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtDeviceGetStreamPriorityRange);
    return aclrtDeviceGetStreamPriorityRangeImpl(leastPriority, greatestPriority);
}

aclError aclrtGetDeviceCapability(int32_t deviceId, aclrtDevFeatureType devFeatureType, int32_t *value)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtGetDeviceCapability);
    return aclrtGetDeviceCapabilityImpl(deviceId, devFeatureType, value);
}

aclError aclrtCtxGetCurrentDefaultStream(aclrtStream *stream)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtCtxGetCurrentDefaultStream);
    return aclrtCtxGetCurrentDefaultStreamImpl(stream);
}

aclError aclrtReduceAsync(void *dst, const void *src, uint64_t count, aclrtReduceKind kind, aclDataType type,
    aclrtStream stream, void *reserve)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtReduceAsync);
    return aclrtReduceAsyncImpl(dst, src, count, kind, type, stream, reserve);
}

aclError aclrtSetDeviceWithoutTsdVXX(int32_t deviceId)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtSetDeviceWithoutTsdVXX);
    return aclrtSetDeviceWithoutTsdVXXImpl(deviceId);
}

aclError aclrtResetDeviceWithoutTsdVXX(int32_t deviceId)
{
    ACL_PROFILING_REG(acl::AclProfType::AclrtResetDeviceWithoutTsdVXX);
    return aclrtResetDeviceWithoutTsdVXXImpl(deviceId);
}

const char *aclrtGetSocName()
{
    return aclrtGetSocNameImpl();
}
